package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;

	/**
	 * 将音频文件上传到腾讯云点播平台
	 * @param trackFile
	 * @return mediaFileId:声音媒体唯一标识  mediaUrl:声音媒体在线地址
	 */
	@Operation(summary = "将音频文件上传到腾讯云点播平台")
	@PostMapping("/trackInfo/uploadTrack")
	public Result<Map<String,String>> uploadTrack(@RequestParam("file") MultipartFile trackFile){
		Map<String,String> map = vodService.uploadTrack(trackFile);
		return Result.ok(map);
	}

	/**
	 * TODO:该接口需要登录才能访问
	 * 保存声音
	 * @param trackInfoVo 声音信息vo对象
	 * @param userId 用户ID
	 * @retrun
	 */
	@GuiGuLogin
	@Operation(summary = "保存声音")
	@PostMapping("/trackInfo/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo){
		Long userId = AuthContextHolder.getUserId();
		trackInfoService.saveTrackInfo(trackInfoVo,userId);
		return Result.ok();
	}

	/**
	 * TODO:该接口需要登录才能访问
	 * 获取当前用户声音分页列表
	 * @param page
	 * @param limit
	 * @param trackInfoQuery 请求体
	 * @return
	 */
	@GuiGuLogin
	@Operation(summary = "获取当前用户声音分页列表")
	@PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
	public Result<Page<TrackListVo>> getUserTrackPage(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody TrackInfoQuery trackInfoQuery){
		// 1. 获取当前用户ID
		Long userId = AuthContextHolder.getUserId();
		trackInfoQuery.setUserId(userId);
		// 2. 封装MP分页对象
		Page<TrackListVo> pageInfo = new Page<>(page,limit);
		// 3. 调用业务逻辑实现分页查询
		pageInfo  = trackInfoService.getUserTrackPage(pageInfo,trackInfoQuery);
		return Result.ok(pageInfo);
	}

	/**
	 * 查询声音信息数据回显页面
	 * @param id
	 * @return
	 */
	@Operation(summary = "查询声音信息数据回显页面")
	@GetMapping("/trackInfo/getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable Long id){
		TrackInfo trackInfo = trackInfoService.getById(id);
		return Result.ok(trackInfo);
	}

	/**
	 * 修改声音信息
	 * @param id
	 * @param trackInfoVo
	 * @return
	 */
	@Operation(summary = "修改声音信息")
	@PutMapping("/trackInfo/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable Long id,@RequestBody TrackInfoVo trackInfoVo){
		trackInfoService.updateTrackInfo(id,trackInfoVo);
		return Result.ok();
	}

	/**
	 * 删除声音信息
	 * @param id
	 * @return
	 */
	@Operation(summary = "删除声音信息")
	@DeleteMapping("/trackInfo/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id){
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

	/**
	 * 分页获取当前用户查看专辑的声音列表（动态判断付费标识）
	 * @param albumId
	 * @param page
	 * @param limit
	 * @return
	 */
	@GuiGuLogin(require = false)
	@Operation(summary = " 分页获取当前用户查看专辑的声音列表（动态判断付费标识）")
	@GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<Page<AlbumTrackListVo>> getAlbumTrackPage(@PathVariable Long albumId,@PathVariable Integer page,@PathVariable Integer limit){
		// 1. 获取用户ID
		Long userId = AuthContextHolder.getUserId();
		// 2. 构建分页对象
		Page<AlbumTrackListVo> pageInfo = new Page<>(page,limit);
		// 3. 调用业务逻辑进行分页
		pageInfo = trackInfoService.getAlbumTrackPage(userId,albumId,pageInfo);
		return Result.ok(pageInfo);
	}

	/**
	 * 根据声音ID查询声音统计信息
	 * @param trackId
	 * @return
	 */
	@Operation(summary = "根据声音ID查询声音统计信息")
	@GetMapping("/trackInfo/getTrackStatVo/{trackId}")
	public Result<AlbumStatVo> getTrackStatVo(@PathVariable Long trackId){
		AlbumStatVo albumStatVo =trackInfoService.getTrackStatVo(trackId);
		return Result.ok(albumStatVo);
	}


	/**
	 * 获取用户声音分集购买支付列表
	 * @param trackId 选择“付费标识”声音ID
	 * @return [{name:"本集",price:"1",trackCount:1},{name:"后10集",price:"10",trackCount:10},{name:"全集",price:"15",trackCount:15}]
	 */
	@Operation(summary = "获取用户声音分集购买支付列表")
	@GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
	@GuiGuLogin
	public Result<List<Map<String,Object>>> getUserWaitBuyTrackList(@PathVariable Long trackId){
		List<Map<String,Object>> list = trackInfoService.getUserWaitBuyTrackList(trackId);
		return Result.ok(list);
	}


	/**
	 * 查询当前用户待购买声音列表作为待结算商品列表
	 * @param trackId 选择购买声音ID
	 * @param trackCount 购买集数
	 * @return
	 */
	@Operation(summary = "查询当前用户待购买声音列表作为待结算商品列表")
	@GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
	@GuiGuLogin
	public Result<List<TrackInfo>> getPaidTrackInfoList(@PathVariable Long trackId,@PathVariable Integer trackCount){
		List<TrackInfo> list = trackInfoService.getPaidTrackInfoList(trackId,trackCount);
		return Result.ok(list);
	}

}

